alter FUNCTION f_finrptmulticolumnsdtl
(@AMULTIBARID INT) RETURNS @TempTable TABLE (SUBID INT, PSUBID INT, PBODEBIT INT) AS
BEGIN
  DECLARE @SUBID INT
  DECLARE @PSUBID INT
  DECLARE @PBODEBIT INT
  DECLARE TempCursor1 CURSOR LOCAL FORWARD_ONLY STATIC OPTIMISTIC FOR SELECT A.SUBID, A.SUBID, A.boDEBIT 
    FROM tfd_multicolumnsdtl A LEFT JOIN tfd_subject s ON s.subjectid = A.SUBID WHERE s.subnode=1 AND A.KEYID = @AMULTIBARID
  OPEN TempCursor1
  FETCH NEXT FROM TempCursor1 INTO @SUBID, @PSUBID, @PBODEBIT
  WHILE (@@FETCH_STATUS = 0)
    BEGIN
      INSERT INTO @TempTable VALUES (@SUBID, @PSUBID, @PBODEBIT)
      FETCH NEXT FROM TempCursor1 INTO @SUBID, @PSUBID, @PBODEBIT
    END
  CLOSE TempCursor1
  DEALLOCATE TempCursor1
  DECLARE TempCursor2 CURSOR LOCAL FORWARD_ONLY STATIC OPTIMISTIC FOR SELECT A.SUBID, A.BODEBIT FROM tfd_multicolumnsdtl A LEFT JOIN tfd_subject s ON s.subjectid = A.SUBID WHERE s.subnode=0 AND A.KEYID = @AMULTIBARID
  OPEN TempCursor2
  FETCH NEXT FROM TempCursor2 INTO @PSUBID, @PBODEBIT
  WHILE (@@FETCH_STATUS = 0)
    BEGIN
      DECLARE TempCursor3 CURSOR LOCAL FORWARD_ONLY STATIC OPTIMISTIC FOR SELECT subjectid AS SUBID FROM tfd_subject WHERE subcode LIKE (SELECT subcode + '%' FROM tfd_subject WHERE subjectid = @PSUBID) AND subnode=1
      OPEN TempCursor3
      FETCH NEXT FROM TempCursor3 INTO @SUBID
      WHILE (@@FETCH_STATUS = 0)
        BEGIN
          INSERT INTO @TempTable VALUES (@SUBID, @PSUBID, @PBODEBIT)
          FETCH NEXT FROM TempCursor3 INTO @SUBID
        END
      CLOSE TempCursor3
      DEALLOCATE TempCursor3
      FETCH NEXT FROM TempCursor2 INTO @PSUBID, @PBODEBIT
    END
  CLOSE TempCursor2
  DEALLOCATE TempCursor2
  RETURN
END